%{
#include "bisonUtils.h"
#include "symbolMgt.h"
#include "treeMgt.h"
#include <stdio.h>
extern int yylex();
extern void yyerror(char *s);
%}

%token '.' '=' '(' ')' '"' ',' ';' 
%token <stringVal> KEYWORD   
%token <stringVal> SYMBOL
%token <stringVal> STRINGVALUE

%union {
	char * stringVal;
}
%%
Program: 
    | Program Instruction

Instruction: NodeDef { printf("NodeDef"); }
       | NodeAdd { printf("NodeAdd"); }
       | TreeDef { printf("TreeDef"); }

NodeDef: KEYWORD SYMBOL '=' KEYWORD KEYWORD '(' STRINGVALUE ')' ';' {
   if (isInSymbolTable($2) == 1) {
      /* On signale l'erreur et on n'arrête pas */
      printf("Ce symbole (%s) est déjà dans la table\n",$2);
   } 
   TreeNode *tree = newTreeNode($7); 
   insertInSymbolTable($2, TREE_NODE);
   setRefForSymbolInTable($2,tree);
   printf("Debug: NodeDef: newTreeNode(%s) [pointeur:%p] ; insertInSymbolTable(%s) ; setRefForSymbolInTable(%s,%p)\n",$7,tree,$2,$2,tree);
}

NodeAdd: KEYWORD SYMBOL '=' KEYWORD KEYWORD '(' SYMBOL ')' ';' { 
   if (isInSymbolTable($2) == 1) {
      /* On signale l'erreur et on n'arrête pas */
      printf("Ce symbole (%s) est déjà dans la table\n",$2);
   }
   if (isInSymbolTable($7) == 0) {
      printf("Ce symbole (%s) référencant l'arbre n'existe pas dans la table\n",$7);
   } 
   Tree *tree = newTree((TreeNode *) getRefForSymbolInTable($7)); 
   insertInSymbolTable($2, TREE_STRUCT);
   setRefForSymbolInTable($2,tree);
   printf("Debug: NodeAdd: newTree(%p); insertInSymbolTable(%s) ; setRefForSymbolInTable(%s,%p)\n",getRefForSymbolInTable($7),$2,$2,tree);
}

TreeDef: SYMBOL '.' KEYWORD '(' SYMBOL ',' SYMBOL ')' ';' {
    printf("Debug: Set Parent %p Child %p \n",getRefForSymbolInTable($5),getRefForSymbolInTable($7));
    addChildToParentNode((TreeNode *) getRefForSymbolInTable($5),(TreeNode *) getRefForSymbolInTable($7));
}


%%
